package com.example.httprequest.resttmplate;

import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.net.URISyntaxException;

/**
 * @Author:     Yudh
 * @Date:       ${DATE} ${HOUR}:${MINUTE}
 * @Description:
 **/
@Slf4j
@Component
public class TestService {

    @Autowired
    private RestTemplate restTemplate;

    public void testForGet() throws Exception {
        String uri = "http://localhost:8080/test01..";
        // 1-getForObject()
        User user1 = this.restTemplate.getForObject(uri, User.class);

        // 2-getForEntity()
        ResponseEntity<User> responseEntity1 = this.restTemplate.getForEntity(uri, User.class);
        HttpStatusCode statusCode = responseEntity1.getStatusCode();
        HttpHeaders header = responseEntity1.getHeaders();
        User user2 = responseEntity1.getBody();

        // 3-exchange()
        RequestEntity requestEntity = RequestEntity.get(new URI(uri)).build();
        ResponseEntity<User> responseEntity2 = this.restTemplate.exchange(requestEntity, User.class);
        User user3 = responseEntity2.getBody();
    }

    public void testForPost() throws Exception {
        String uri = "http://localhost:8080/test01..";
        // 1-getForObject()
        User user1 = this.restTemplate.getForObject(uri, User.class);

        // 2-getForEntity()
        ResponseEntity<User> responseEntity1 = this.restTemplate.getForEntity(uri, User.class);
        HttpStatusCode statusCode = responseEntity1.getStatusCode();
        HttpHeaders header = responseEntity1.getHeaders();
        User user2 = responseEntity1.getBody();

        // 3-exchange()
        RequestEntity requestEntity = RequestEntity.get(new URI(uri)).build();
        ResponseEntity<User> responseEntity2 = this.restTemplate.exchange(requestEntity, User.class);
        User user3 = responseEntity2.getBody();
    }

    public void testForPost2() throws Exception {
        String url = "http://demo/api/book/";
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        String requestJson = "{...}";
        HttpEntity<String> entity = new HttpEntity<String>(requestJson,headers);
        String result = restTemplate.postForObject(url, entity, String.class);
        log.info("result:{}", result);
    }

    public void testForPost3() throws Exception {

        RestTemplate restTemplate = new RestTemplate();
        String url = "http://47.xxx.xxx.96/register/checkEmail";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
        map.add("email", "844072586@qq.com");

        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity( url, request , String.class );
        log.info(response.getBody());
    }
}
